import { baseUrl, FetchUrl } from './url'
import Axios from 'axios'
import { history } from 'umi'
import { message } from 'antd'
const {
  sendLoginUrl,
  sendLogoutUrl,
  ListFirstLevelMenuForUserUrl,
  listOthersMenuForUserUrl,
  getActivityListUrl,
  batchDeleteActivityUrl,
  addOrUpdateActivityUrl,
  queryAddressForFourLinkageUrl,
  pictureUploadUrl,
  saveDraftsUrl,
  queryByUpdateUrl,
  lotteryWhiteList,
  pageByCandidateQuery,
  updateByCondition,
  downloadCandidateTemplate,
  importCandidates,
  searchOrganizationData,
} = FetchUrl
import {
  userInfo,
  SaveDrafts,
  SendWhiteListParams,
  SendUpdateByCondition,
  SendPageByCandidateQuery,
} from './index.d'

// 登录
export function loginMyIdTo(params: userInfo) {
  return sendRequest(sendLoginUrl, 'POST', params)
}
// 登出
export function logoutMyIdTo() {
  return sendRequest(sendLogoutUrl, 'POST', {})
}
// 获取一级路由
export function getListFirstLevelMenuForUser() {
  return sendRequest(ListFirstLevelMenuForUserUrl, 'POST', {})
}
// 获取二级路由
export function getListOthersMenuForUserUrl(params: any) {
  return sendRequest(listOthersMenuForUserUrl, 'POST', params)
}
// 获取创建活动列表
export function getActivityList(params: any) {
  return sendRequest(getActivityListUrl, 'POST', params)
}
// 删除创建活动列表
export function removeActivity(params: any) {
  return sendRequest(batchDeleteActivityUrl, 'POST', params)
}
// 新增或修改创建活动列表
export function UpdateActivity(params: any) {
  return sendRequest(addOrUpdateActivityUrl, 'POST', params)
}
// 新增或修改创建活动草稿
export function saveDrafts(params: SaveDrafts) {
  return sendRequest(saveDraftsUrl, 'POST', params)
}
// 活动详情查询
export function queryByUpdate(params: any) {
  return sendRequest(queryByUpdateUrl, 'POST', params)
}
// 获取省市区
export function getQueryAddress(params: any) {
  return sendRequest(queryAddressForFourLinkageUrl, 'POST', params)
}
// 上传图片
export function sendPictureUpload(params: any, header: any) {
  return sendRequest(pictureUploadUrl, 'POST', params, header)
}
//白名单
export function sendWhiteList(params: SendWhiteListParams) {
  return sendRequest(lotteryWhiteList, 'POST', params)
}
//白名单列表查询
export function sendPageByCandidateQuery(params: SendPageByCandidateQuery) {
  return sendRequest(pageByCandidateQuery, 'POST', params)
}
//白名单修改
export function sendUpdateByCondition(params: SendUpdateByCondition) {
  return sendRequest(updateByCondition, 'POST', params)
}
//白名单下载
export function sendDownloadCandidateTemplate(params: any) {
  // return sendRequest(downloadCandidateTemplate, 'POST', params, header);

  return Axios({
    method: 'post',
    url: baseUrl + downloadCandidateTemplate,
    params: params,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/octet-stream',
    },
  })
}
//白名单导入
export function sendImportCandidates(params: any) {
  return sendRequest(importCandidates, 'POST', params)
}

// 组织架构
export function sendSearchOrganizationData() {
  return fetch(searchOrganizationData, {
    method: 'post',
  }).then(res => res.json())
}

function sendRequest(url: string, method: string, body?: any, header?: any) {
  let params
  let defaultHeader = { 'Content-type': 'application/json' }
  if (method.toUpperCase() === 'GET') {
    url += '?'
    let keyList = Object.keys(body)
    keyList.forEach((item, index) => {
      if (body[item].constructor.name === 'Array') {
        body[item].forEach((dataI: any, indexD: number) => {
          url += `${item}[${indexD}]=${dataI}${
            indexD !== body[item].length - 1 ? '&' : ''
          }`
        })
        if (index !== keyList.length - 1 && body[item].length > 0) {
          url += '&'
        }
      } else {
        url += item + '=' + body[item]
        if (index !== keyList.length - 1) {
          url += '&'
        }
      }
    })
    params = undefined
  } else {
    params = {
      method,
      headers: header || defaultHeader,
      body: header ? body : JSON.stringify(body),
    }
  }
  return fetch(baseUrl + url, params)
    .then(res => res.json())
    .then(res => {
      if (res.code === 302) {
        message.error('身份验证过期，请重新登录！')
        localStorage.removeItem('user')
        history.push('/')
      }
      return res
    })
    .catch(err => {
      console.log(err)
    })
}

//
